<?php
/**
 * Description: 新品参与投票
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/5/20
 * Time: 15:40
 */

use Biz\model\NewProduct\NewProductJoinRecordModel;
use Biz\model\NewProduct\NewProductWikiModel;
use Doraemon\cache\UserCounter;
use Doraemon\model\Member as dbUser;
use Doraemon\service\Follow;
use Doraemon\service\User;

class JoinRecordAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;


    /**
     * 添加支持记录
     * @throws Exception
     */
    public function main()
    {
        //查询符合展示时间的wiki
        $type = $this->getRequest()->getPost('type', 0);
        $nid = $this->getRequest()->getPost('nid', 0);
        $tag_id = $this->getRequest()->getPost('tag_id', 0);
        $wiki_id = $this->getRequest()->getPost('wiki_id', 0);
        $brand_id = $this->getRequest()->getPost('brand_id', 0);
        $basic_id = $this->getRequest()->getPost('basic_id', 0);
        $uid = $this->uid ?: 0;
        if (!$type || !$nid || !$tag_id || !$uid || !$wiki_id || !$brand_id || !$basic_id) {
            throw new Exception('非法参数！');
        }
        $key = $uid . $nid . $type . '_new_product';
        //5秒禁止提交
        $lock = \Biz\cache\brand\BrandShowActivity::isLock(md5($key), 5);
        if ($lock) {
            throw new \UnexpectedValueException('请不要频繁提交');
        }
        try {
            Follow::FollowMultiUsers($uid, [User::HAOHAOZHUFULIGUAN_UID], 'new_product');//关注好好住福利官
        } catch (\Exception $e) {
        }
        $is_have = NewProductJoinRecordModel::first(['uid' => $uid, 'nid' => $nid]);

        if ($is_have) {
            throw new \Exception('「谢谢你的喜欢」！');
        }
        $user_type = dbUser::getUser($uid)['type'];

        $fans = UserCounter::getOne($uid)['fans'] ?? 0;
        $data = [];
        //甄选数据
        if ((int)$type === NewProductWikiModel::GRACE_PDT) {
            $tag = explode(',', $tag_id);
            foreach ($tag as $k => $v) {
                $data[$k]['tid'] = $v;
                $data[$k]['uid'] = $uid;
                $data[$k]['wiki_id'] = $wiki_id;
                $data[$k]['brand_id'] = $brand_id;
                $data[$k]['basic_id'] = $basic_id;
                $data[$k]['nid'] = $nid;
                $data[$k]['fans'] = $fans;
                $data[$k]['type'] = $type;
                $data[$k]['user_type'] = $user_type;
            }
        } else {
            //新品数据
            $data['uid'] = $uid;
            $data['wiki_id'] = $wiki_id;
            $data['basic_id'] = $basic_id;
            $data['brand_id'] = $brand_id;
            $data['nid'] = $nid;
            $data['tid'] = $tag_id;
            $data['fans'] = $fans;
            $data['type'] = $type;
            $data['user_type'] = $user_type;
        }
        //添加数据
        NewProductJoinRecordModel::insert($data);
        $end_time = NewProductWikiModel::value(['id' => $nid], 'end_time');
//        if ((int)$type === NewProductWikiModel::GRACE_PDT) {
//            $add_day = '+1 day';
//        } else {
//            $add_day = '+1 day';
//        }
        $day = date("m.d", strtotime($end_time));
        $msg = '「谢谢你的反馈，已成功参与抽奖，请关注' . $day . '开奖通知」';
        $log_data['wiki_id'] = $wiki_id;
        $log_data['basic_id'] = $basic_id;
        \Doraemon\tools\ActionLog::log($uid, $log_data, \Doraemon\tools\ActionLog::ACTIVITY_NEW_PRODUCT_LIKE);
        $this->setSuccess(['msg' => $msg]);
    }
}